package com.javaee.controller;

import com.javaee.entity.Activity;
import com.javaee.entity.Student;
import com.javaee.entity.User;
import com.javaee.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.List;

@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping
    public Result add(@RequestBody User user) {
        boolean flag = userService.add(user);
        return new Result(flag ? Code.ADD_OK:Code.ADD_ERR,flag);
    }

    @GetMapping("/id/{id}")
    public Result getById(@PathVariable int id){
        User user= userService.getById(id);
        Integer code = user != null ? Code.GET_OK : Code.GET_ERR;
        String msg = user != null ? "查询成功" : "数据查询失败，请重试！";
        return new Result(code,user,msg);
    }

    @GetMapping("/name/{name}")
    public Result getByName(@PathVariable String name){
        User user= userService.getByName(name);
        Integer code = user != null ? Code.GET_OK : Code.GET_ERR;
        String msg = user != null ? "查询成功" : "数据查询失败，请重试！";
        return new Result(code,user,msg);
    }
}
